package com.yzly.demo.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

/**
 * 根据request获取body中的JSON对象
 */
public class RequestGetJsonBody {


    public static JSONObject getJsonByRequest(HttpServletRequest req) {
        JSONObject json = null;
        try {
            BufferedReader reader = req.getReader();
            String input;
            StringBuffer requestBody = new StringBuffer();
            while ((input = reader.readLine()) != null) {
                requestBody.append(input);
            }
            String body = requestBody.toString();
            json = (JSONObject) JSONObject.parse(body);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return json;
        }

    }
}
